var prepare = (function () {
  // 预先数据请求地址映射
  var prepareUrlMap = {
    resume: "person/resume/prepare",
    index: "person/index/prepare",
    qualification: "enterprise/qualification/prepare",
    recruit: "enterprise/recruit/prepare",
    enterpriseResume: "enterprise/index/prepare"
  };

  // 预先数据
  var prepareData = {
    abilityList: null, // 语言掌握列表
    educationList: null, // 教育程度列表
    experienceList: null, // 工作经验列表
    natureList: null, // 工作性质列表
    languageList: null, // 语种列表
    positionList: null, // 职位分类列表
    regionList: null, // 地区列表
    resumeTagList: null, // 标签列表
    sizeList: null, // 公司规模列表
    statusList: null, // 工作状态列表
    typeList: null, // 公司性质列表
    tradeList: null, // 行业分类列表
    welfareList: null, // 岗位福利标签列表
    experience_configList: null // 工作年限设置
  };

  // 预先数据字段名与请求数据字段名映射
  var prepareKeyMap = Object.keys(prepareData).reduce(function (map, val) {
    var key = val.replace("List", "");
    map[key] = val;
    return map;
  }, {});

  // 处理请求或者session中的获取到的数据，放到预先数据中
  function disposePrepareData(data, callback) {
    Object.keys(data).forEach(function (key) {
      prepareData[prepareKeyMap[key] || key] = data[key];
    });
    typeof callback == "function" && callback(data);
  }

  // 获取预先数据
  function getPrepareData(type, callback) {
    var type = type || "index";
    var url = prepareUrlMap[type];
    var dataStr = sessionStorage.getItem(url);
    if (dataStr) {
      disposePrepareData(JSON.parse(dataStr), callback);
    } else {
      request(url, "GET", "", function (res) {
        var data = res.data;
        sessionStorage.setItem(url, JSON.stringify(data));
        disposePrepareData(data, callback);
      });
    }
  }

  function getIndexPrepare(callback) {
    getPrepareData("index", function (data) {
      prepareData.welfareList = data.tag;
      typeof callback == "function" && callback(data);
    });
  }

  function getResumePrepare(callback) {
    getPrepareData("resume", function (data) {
      prepareData.resumeTagList = data.tag;
      typeof callback == "function" && callback(data);
    });
  }

  function getQualificationPrepare(callback) {
    getPrepareData("qualification", callback);
  }

  function getRecruitPrepare(callback) {
    getPrepareData('recruit', callback)
  }

  function getEnterPriseResumeData(callback) {
    getPrepareData('enterpriseResume', callback)
  }

  var simpleNameKeyList = [
    "ability",
    "education",
    "experienc",
    "language",
    "nature",
    "resumeTag",
    "size",
    "status",
    "type",
    "welfare",
    "tag",
  ];

  var nestNameKeyList = [
    'position',
    'region',
    'trade',
  ]

  var betweenNameKeyList = [
    'experience_config',
    'experience'
  ]

  function getSimpleName(id, list) {
    list = list || []
    for (var i = 0; i < list.length; i++) {
      if (list[i].id == id) {
        return list[i].name;
      }
    }
    return "";
  }

  function getBetweenName(id, list) {
    list = list || [];
    for (var i = 0; i < list.length; i++) {
      if (list[i].id == id) {
        return list[i].min + '~' + list[i].max;
      }
    }
    return ""
  }

  var ins = {
    prepareData: prepareData,
    getPrepareData: getPrepareData,
    getIndexPrepare: getIndexPrepare,
    getResumePrepare: getResumePrepare,
    getQualificationPrepare: getQualificationPrepare,
    getRecruitPrepare: getRecruitPrepare,
    getEnterPriseResumeData: getEnterPriseResumeData
  };


  simpleNameKeyList.reduce(function (map, key) {
    var methodKey = "get" + key[0].toUpperCase() + key.substring(1) + "Name";
    var listKey = prepareKeyMap[key];
    map[methodKey] = function getName(id, list) {
      return getSimpleName(id, list || prepareData[listKey]);
    };
    return map;
  }, ins);

  nestNameKeyList.reduce(function (map, key) {
    var methodKey = "get" + key[0].toUpperCase() + key.substring(1) + "NameList";
    var listKey = prepareKeyMap[key];
    var nameKey = key === 'region' ? 'region_name' : 'name'
    function getNameList(id, list, result) {
      list = list || prepareData[listKey] || [];
      result = result || [];
      var item, val;
      for (var i = 0; i < list.length; i++) {
        item = list[i];
        if (item.id === id) {
          result.push(item[nameKey])
          return result;
        } else if (item.children) {
          getNameList(id, item.children, result);
          if (result.length) {
            result.unshift(item[nameKey]);
            return result
          }
        }
      }
      return result;
    }
    map[methodKey] = getNameList;
    return map
  }, ins)

  betweenNameKeyList.reduce(function(map, key) {
    var methodKey = "get" + key[0].toUpperCase() + key.substring(1) + "Name";
    var listKey = prepareKeyMap[key];
    map[methodKey] = function getName(id, list) {
      return getBetweenName(id, list || prepareData[listKey]);
    };
    return map;
  }, ins)

  return ins;
})();

